/**
 * Created by 白雨浓 on 2018/1/24 9:42.
 */

import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default {
    update(self, name, fields){
        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'update.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('update模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        const variables = [];

        const debars = ['id', 'create_time', 'update_time'];

        let id = '';

        fields.forEach(field => {
            if (debars.indexOf(field.COLUMN_NAME) < 0) {
                if (field.COLUMN_NAME.indexOf('id') >= 0) {
                    id = field.COLUMN_NAME;
                } else {
                    variables.push({sName: field.COLUMN_NAME, jName: self.$str.toCamel(field.COLUMN_NAME)})
                }
            }
        });

        const tmp = {
            dbName: self.config.dbName,
            tableName: name,
            variables: variables,
            sWhere: id,
            jWhere: self.$str.toCamel(id)
        };

        return ejs.render(tmpFile, tmp);
    }
}